package com.lechuang.controller.validate;

import com.lechuang.kit.StringKit;
import com.lechuang.vo.BaseResult;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by Administrator on 2016/9/27.
 */
public class BaseValidator {
     HttpServletRequest request;
    protected static String errorMsg = "1111";
    public BaseValidator(){

    }
    public  void validate(HttpServletRequest request){
        this.request = request;
    }

    protected void validateRequireString(String field,String errKey,String errMsg){
        String para = getPara(field);
        boolean notBlank = StringKit.isNotBlank(para);
        if(!notBlank){
            addError(errKey,errMsg);
        }
    }

    protected void addError(String errKey,String errMsg){
        BaseResult.addError(errKey,errMsg);
    }

    protected String getPara(String param){
        String parameter = request.getParameter(param);
        return parameter;
    }

    protected String getParaToString(String param){
        String para = getPara(param);
        boolean notBlank = StringKit.isNotBlank(para);
        if(!notBlank){
            addError(errorMsg,para+"不存在");
        }
        return para;
    }
}
